#ifndef __QUADTREE_H__
#define __QUADTREE_H__

#include "Object.h"
#include <vector>

using std::vector;

#define LIM_W_H 5
namespace Advanced2D
{
	class QuadNode
	{
	public:
		vector<Object*> listObj;

		RECT*		_rect;
		QuadNode*	LT,*RT,*LB,*RB;
		QuadNode()
		{
			_rect=NULL;
			LT=RT=LB=RB=NULL;
		}
		QuadNode(int left,int top,int width,int height)
		{
			_rect=new RECT();
			_rect->left=left;
			_rect->top=top;
			_rect->right=_rect->left+width;
			_rect->bottom=_rect->top+height;
			LT=RT=LB=RB=NULL;
		}
	};

	class QuadTree
	{
	public:
		bool CheckRECTInRECT(RECT r,RECT rect);
		bool CheckPointInRECT(int x,int y,RECT rect);
		void GetObjInRec(QuadNode* root,RECT Screen,vector<Object*> &result);

		QuadNode*	_pRoot;
		int			_mapWidth;
		int			_mapHeight;
		int			_count;

		QuadTree(int w,int h);
		~QuadTree(void);

		void Add(QuadNode *root,Object *Ob);
		void OutPutTree(QuadNode *root);
		QuadNode* getTreeRoot(){ return _pRoot; };
		//Chi in nhung cai nam tren main hinh hien tai
		void DrawObjects(RECT Screen);
		Object* CheckCollision(QuadNode *root,RECT r,int Seq);
		void CheckCollision(QuadNode *root,RECT r,vector<Object*>&result);
		bool IsOnObject(RECT *rect,int Seq);//duoi chan object la object khac hay la khoang trong
		void UpdatePosition(QuadNode *root,bool& OK);
		void GetAllObj(QuadNode *root,vector<Object*> &result);
		void RemoveAll(QuadNode *root);
	};

}


#endif